package com.wn.excel.handler;

import com.wn.excel.config.ExcelProperties;
import com.wn.excel.config.ExportTableProperties;
import com.wn.excel.constant.ExcelConst;
import com.wn.excel.model.ColumnMapping;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @FileName: WorkBookUtils
 * @Description: 工作簿工具类
 * @Author: MaWenHao
 * @Created: 2020/3/2 - 23:47
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@Component
public class WorkBookHandler {

    @Autowired
    private ExcelProperties excelProperties;

    @Autowired
    private ExportTableProperties exportTableProperties;

    /**
     * 创建工作簿
     */
    public HSSFWorkbook createWorkBook() {
        return new HSSFWorkbook();
    }

    public HSSFSheet createSheet(HSSFWorkbook workbook, String sheetName) {
        return workbook.createSheet(sheetName);
    }


    public HSSFCellStyle createStyle(HSSFWorkbook wb) {
        return wb.createCellStyle();
    }

    /**
     * 设置默认样式
     *
     * @param wb
     * @param sheetIndex
     */
    public void setWorkBookStyle(HSSFWorkbook wb, int sheetIndex) {
        int cellCount = exportTableProperties.getColumnMapping().size() + 1;
        List<ColumnMapping> columnMapping = exportTableProperties.getColumnMapping();
        HSSFSheet sheet = wb.getSheetAt(sheetIndex);

        // 设置默认行高
        sheet.setDefaultRowHeightInPoints(excelProperties.getDefaultRowHigh());
        // 设置第一列列宽
        sheet.setColumnWidth(0, excelProperties.getFirstCellWidth() * ExcelConst.COLUMN_WIDTH_UNIT);
        for (int i = 0; i < cellCount; i++) {
            if (i > 0) {
                ColumnMapping mapping = columnMapping.get(i - ExcelConst.SKIP);
                sheet.setColumnWidth(i, mapping.getCellWidth() * ExcelConst.COLUMN_WIDTH_UNIT);
            }
        }

    }

    public HSSFCellStyle getTitleStyle(HSSFWorkbook wb) {
        return this.getStyle(wb,
                excelProperties.getTitleFont(),
                excelProperties.getTitleFontSize(),
                excelProperties.getTitleBackgroundColor(),
                true);
    }

    public HSSFCellStyle getTopStyle(HSSFWorkbook wb) {
        return this.getStyle(wb,
                excelProperties.getTopFont(),
                excelProperties.getTopFontSize(),
                excelProperties.getTopBackgroundColor(),
                true);
    }

    public HSSFCellStyle getHeaderStyle(HSSFWorkbook wb) {
        return this.getStyle(wb,
                excelProperties.getHeaderFont(),
                excelProperties.getHeaderFontSize(),
                excelProperties.getHeaderBackgroundColor(),
                true);
    }

    public HSSFCellStyle getContentStyle(HSSFWorkbook wb) {
        return this.getStyle(wb,
                excelProperties.getContentFont(),
                excelProperties.getContentFontSize(),
                excelProperties.getContentBackgroundColor(),
                false);
    }

    public HSSFCellStyle getBottomStyle(HSSFWorkbook wb) {
        return this.getStyle(wb,
                excelProperties.getBottomFont(),
                excelProperties.getBottomFontSize(),
                excelProperties.getBottomBackgroundColor(),
                true);
    }

    public HSSFCellStyle getStyle(HSSFWorkbook wb, String fontName, short fontSize, short backgroundColor, boolean isCenter) {
        HSSFCellStyle style = wb.createCellStyle();
        //设置单元格格式
        HSSFFont font = wb.createFont();
        font.setFontName(fontName);
        font.setFontHeightInPoints(fontSize);//设置字体大小
        style.setFont(font);

        // 设置边框
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框

        //setStyle单元格居中
        if (isCenter) {
            style.setAlignment(HorizontalAlignment.CENTER); // 居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        }
        style.setFillForegroundColor(backgroundColor);// 背景颜色
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        return style;
    }
}
